// HSS.Forms.ViewSettings.cs
// ----------------------------------------------------------------------------
// Licensed under the MIT license
// http://www.opensource.org/licenses/mit-license.html
// ----------------------------------------------------------------------------
// HighSpeed-Solutions, LLC
// Copyright (c) 2001-2010
// ----------------------------------------------------------------------------
// File:       ViewSettings.cs
// Author:     HSS\gbanta
// Created:    08/12/2010
// Modified:   12/04/2010
// ----------------------------------------------------------------------------
namespace HSS.Forms
{
	#region Using Directives
	using System;
	using System.Windows.Forms;
	#endregion

	#region ViewSettings
	/// <summary>
	/// DataList View Settings
	/// </summary>
	[Serializable]
	public sealed class ViewSettings
	{
		#region Constructors
		/// <summary>
		/// ctor
		/// </summary>
		public ViewSettings()
		{
			this._name = Guid.NewGuid().ToString();
			this._view = View.Details;
			this._arrangedBy = 0;
			this._allowShowGroups = true;
			this._grouped = false;
			this._sorted = false;
			this._autoSizeColumns = false;
			this._persist = false;
		}
		/// <summary>
		/// ctor
		/// </summary>
		/// <param name="name">The Name to reference the config by</param>
		/// <param name="view">The View of the View these config are for</param>
		/// <param name="arrangedBy">The Column Index the View is sorted/arrange by</param>
		/// <param name="allowShowGroups">Indicate whether or not the View can show groups</param>
		/// <param name="isGrouped">Indicates whether or not the View is Grouped</param>
		/// <param name="allowSort">Indicate whether or not the View can sort</param>
		/// <param name="isSorted">Indicates whether or not the View is sorted</param>
		/// <param name="autoSizeColumns">Indicates whether or not the View automatically size it's column</param>
		/// <param name="sortOrder">Which direction did we last sort</param>
		/// <param name="persist">Whether or not to persist these setting and should be saved and reused</param>
		public ViewSettings(string name, View view, int arrangedBy, bool allowShowGroups, bool isGrouped,
			bool allowSort, bool isSorted, SortOrder sortOrder, bool autoSizeColumns, bool persist)
		{
			this._name = name;
			this._view = view;
			this._arrangedBy = arrangedBy;
			this._allowShowGroups = allowShowGroups;
			this._grouped = isGrouped;
			this._allowSort = allowSort;
			this._sorted = isSorted;
			this._lastSortOrder = sortOrder;
			this._autoSizeColumns = autoSizeColumns;
			this._persist = persist;
		}
		#endregion

		#region Properties
		/// <summary>
		/// Gets the Name to reference the config by
		/// </summary>
		public string Name
		{
			get { return _name; }
		} string _name;
		/// <summary>
		/// The View of the View these config are for
		/// </summary>
		public View View
		{
			get { return _view; }
			set { _view = value; }
		} View _view;
		/// <summary>
		/// The Column Index the View is sorted/arranged by
		/// </summary>
		public int ArrangedBy
		{
			get { return _arrangedBy; }
			set { _arrangedBy = value; }
		} int _arrangedBy;
		/// <summary>
		/// Indicates whether or not to allow showing the View items in groups
		/// </summary>
		public bool AllowShowGroups
		{
			get { return _allowShowGroups; }
			set { _allowShowGroups = value; }
		} bool _allowShowGroups;
		/// <summary>
		/// Indicates whether or not the View is grouped
		/// </summary>
		public bool IsGrouped
		{
			get { return _grouped; }
			set { _grouped = value; }
		} bool _grouped;
		/// <summary>
		/// Indicates whether or not to allow sorting of the View items
		/// </summary>
		public bool AllowSort
		{
			get { return _allowSort; }
			set { _allowSort = value; }
		} bool _allowSort;
		/// <summary>
		/// Indicates whether or not the View should be sorted
		/// </summary>
		public bool IsSorted
		{
			get { return _sorted; }
			set { _sorted = value; }
		} bool _sorted;
		/// <summary>
		/// Indicates whether or not the View automatically size it's column
		/// </summary>
		public bool AutoSizeColumns
		{
			get { return _autoSizeColumns; }
			set { _autoSizeColumns = value; }
		} bool _autoSizeColumns = true;
		/// <summary>
		/// Which direction did we last sort
		/// </summary>
		public SortOrder LastSortOrder
		{
			get { return _lastSortOrder; }
			set { _lastSortOrder = value; }
		} SortOrder _lastSortOrder;
		/// <summary>
		/// Whether or not to persist the setting
		/// </summary>
		public bool ShouldPersist
		{
			get { return _persist; }
			set { _persist = value; }
		} bool _persist;
		/// <summary>
		/// If this instance is actually Persisted
		/// </summary>
		public bool IsPersisted
		{
			get { return _isPersisted; }
			set { _isPersisted = value; }
		} bool _isPersisted;
		#endregion

		#region Methods
		/// <summary>
		/// Returns the properties as a Pipe '|' delimeted string
		/// </summary>
		/// <returns>The properties as a Pipe '|' delimeted string</returns>
		public override string ToString()
		{
			return string.Join("|", new string[] { this.Name, 
														this.View.ToString(), 
														this.ArrangedBy.ToString(), 
														this.AllowShowGroups.ToString(),
														this.IsGrouped.ToString(),
														this.AllowSort.ToString(),
														this.IsSorted.ToString(),
														this.LastSortOrder.ToString(),
														this.AutoSizeColumns.ToString(),
														this.ShouldPersist.ToString() });
		}
		/// <summary>
		///  Create and initialize a new ViewSetting from a string
		/// </summary>
		/// <param name="s">the string to parse</param>
		/// <returns>An instance of ViewSettings</returns>
		public static ViewSettings FromString(string s)
		{
			ViewSettings newView = null;
			try
			{
				string[] values = s.Split("|".ToCharArray());
				newView = new ViewSettings(values[0],
														(View)Enum.Parse(typeof(View), values[1]),
														int.Parse(values[2]),
														bool.Parse(values[3]),
														bool.Parse(values[4]),
														bool.Parse(values[5]),
														bool.Parse(values[6]),
														(SortOrder)Enum.Parse(typeof(SortOrder), (values[7].ToString())),
														bool.Parse(values[8]),
														bool.Parse(values[9]));
			}
			catch
			{
				newView = new ViewSettings();
			}
			return newView;
		}
		#endregion
	}
	#endregion
}